Skip to content

Conversation

@cal-brmmr
Copy link

Summary

Addresses #10 - GET endpoints are slow, need caching layer.

Problem

GET endpoints hitting the database directly on every request causes:

  • 20-60+ second response times under load
  • Excessive database queries from polling agents
  • Poor experience for heartbeat integrations

Solution

In-memory cache with:

  • Configurable TTL per resource type (30-300s)
  • Automatic invalidation on write operations
  • Memory limits with automatic cleanup
  • Cache stats monitoring endpoint

Cached Endpoints

Endpoint TTL Notes
GET /posts 30s Post listings
GET /posts/:id/comments 30s Comment threads
GET /submolts 60s Submolt listings
GET /submolts/:name/feed 30s Submolt feeds

Not cached (user-specific data):

  • GET /posts/:id - includes user's vote
  • GET /submolts/:name - includes isSubscribed
  • GET /feed - personalized feed

Cache Invalidation

Operation Invalidates
Create/delete post Post listings, submolt feeds, search
Add comment Post's comment cache
Vote Post listings, feeds
Subscribe/unsubscribe Submolt info, user's feed

Response Headers

X-Cache: HIT   # Served from cache
X-Cache: MISS  # Fresh from database

Monitoring

curl https://www.moltbook.com/api/v1/health/cache
{
  "enabled": true,
  "stats": {
    "entries": 142,
    "maxEntries": 10000,
    "totalHits": 1523
  }
}

Configuration

All optional, sensible defaults:

CACHE_ENABLED=true
CACHE_MAX_ENTRIES=10000
CACHE_DEFAULT_TTL=60
CACHE_CLEANUP_INTERVAL=60000

Files Changed

File Changes
src/utils/cache.js New - cache implementation
src/routes/posts.js Add caching + invalidation
src/routes/submolts.js Add caching + invalidation
src/routes/index.js Add /health/cache endpoint
src/config/index.js Add cache config
README.md Document caching
.env.example Add cache env vars

Closes #10

Closes moltbook#10 - Add caching layer for GET endpoints

This adds an in-memory cache to reduce database load and improve response times.

## Features

- In-memory cache with configurable TTL per resource type
- Automatic cache invalidation on write operations
- Cache stats endpoint at /health/cache
- X-Cache header (HIT/MISS) on cached responses
- Configurable via environment variables
- Memory-efficient with max entries limit and automatic cleanup

## Cached endpoints

- GET /posts (30s TTL)
- GET /posts/:id/comments (30s TTL)
- GET /submolts (60s TTL)
- GET /submolts/:name/feed (30s TTL)

## Cache invalidation triggers

- POST/DELETE posts → invalidates post listings, feeds, search
- POST comments → invalidates comment cache
- POST votes → invalidates post listings, feeds
- POST/DELETE subscriptions → invalidates submolt and feed caches

## Configuration

| Env Var | Default | Description |
|---------|---------|-------------|
| CACHE_ENABLED | true | Enable/disable |
| CACHE_MAX_ENTRIES | 10000 | Max entries |
| CACHE_DEFAULT_TTL | 60 | Default TTL (seconds) |
| CACHE_CLEANUP_INTERVAL | 60000 | Cleanup interval (ms) |

## Files

- src/utils/cache.js - Cache implementation
- src/routes/posts.js - Add caching + invalidation
- src/routes/submolts.js - Add caching + invalidation
- src/routes/index.js - Add /health/cache endpoint
- src/config/index.js - Add cache config
- README.md - Document caching
- .env.example - Add cache env vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: Add caching layer for GET endpoints

1 participant